<template>
  <div class="signup-page mx-auto p-3 w-330">
    <h5 class="my-4 text-center">注册者也账户</h5>
    <validate-form @form-submit="onFormsubmit">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          placeholder="请输入邮箱地址"
          type="text"
          v-model="formData.email"
          :rules="emailRules"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">昵称</label>
        <validate-input
          placeholder="请输入昵称"
          type="text"
          v-model="formData.nickName"
          :rules="nameRules"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          type="password"
          placeholder="请输入密码"
          v-model="formData.password"
          :rules="passwordRules"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">重复密码</label>
        <validate-input
          type="password"
          placeholder="请再次密码"
          v-model="formData.repeatPassword"
            :rules="repeatPasswordRules"
        />
      </div>
      <template #submit>
        <button type="submit" class="btn btn-primary btn-block btn-large">注册新用户</button>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import { useRouter } from 'vue-router'
import {signup,signupProps} from '@/api/index'
import ValidateInput,{RuleProps} from '@/components/validateInput/validateInput.vue'
import ValidateForm from '@/components/ValidateForm/ValidateForm.vue'
import createMessage from '@/hooks/createMessage' // 采用函数调用的形式
import {useStore} from 'vuex'
import {GlobalDataProps} from '@/store/index'

export default defineComponent({
  name: 'Signup',
  components: {
    ValidateInput,
    ValidateForm
  },
  setup() {
    const formData = reactive({
      email: '',
      nickName: '',
      password: '',
      repeatPassword: ''
    })

   const router = useRouter()
   const store = useStore<GlobalDataProps>()
    const emailRules: RuleProps = [
      { type: 'required', message: '电子邮箱地址不能为空' },
      { type: 'email', message: '请输入正确的电子邮箱格式' }
    ]
    const nameRules: RuleProps = [
      { type: 'required', message: '昵称不能为空' }
    ]
    const passwordRules: RuleProps = [
      { type: 'required', message: '密码不能为空' },
      {type: 'passwordLen', message: '密码长度不能低于6个字符'}
    ]
    const repeatPasswordRules: RuleProps = [
      { type: 'required', message: '重复密码不能为空' },
      {
        type: 'custom',
        validator: () => {
          return formData.password === formData.repeatPassword
        },
        message: '密码不相同'
      }
    ]

    const onFormsubmit = (result: boolean)=> {
        console.log(result);
        if (result) {
         
            let param:signupProps = {
                email: formData.email,
                nickName: formData.nickName,
                password: formData.password
            }
            signup(param).then(res => {
                console.log('注册成功：',res);
                const {email} = res.data.data
                localStorage.setItem('signupData',email)
                createMessage('注册成功','success')
                setTimeout(()=> {
                    router.push('/login')
                },2000)
                
            }).catch(err => {
                console.log('注册失败：',err);
                createMessage(err,'error')
            })
        }
        
    }

 
  
    return {
        formData,
        onFormsubmit,
        emailRules,
        nameRules,
        passwordRules,
        repeatPasswordRules

    }
  }
})
</script>

<style>
.w-330 {
  max-width: 330px;
}
</style>
